package com.mall.biz.user;

import java.util.Date;
import java.util.Random;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.client.user.InviteCodeService;
import com.mall.common.util.sms.HuYiSmsUtil;
import com.mall.pojo.InviteCode;



/**
 * 验证码服务
 * 
 * @author zhangxianjun
 * @version $Id: VerifyCodeService.java, v 0.1 2015年7月20日 下午10:27:18
 *          zhangxianjun Exp $
 */
@Service
public class VerifyCodeBizImpl {

	private static final String VRCODE_PREFIX = "vrcodeprefix_";
	private static final int EXPIRE = 30 * 60;

	@Autowired
	private HuYiSmsUtil smsUtil;

	@Autowired
	private InviteCodeService inviteCodeService;
	

	private String WHITENO = "88888881";

	/**
	 * 生成验证码
	 * 
	 * @return
	 */
	public String sendVerifyCode(String mobile) {

		InviteCode inviteCode = this.inviteCodeService.getInviteCodeByPhone(mobile);

		if (StringUtils.isBlank(mobile)) {
			return null;
		}

		if (inviteCode != null) {
			String content = "您的验证码是：" + inviteCode.getCode() + "。请不要把验证码泄露给其他人。如非本人操作，可不用理会！";
			boolean result = smsUtil.sendSms(mobile, content);
			if (result) {
				return inviteCode.getCode();
			}
			return "sendmsg error";
		}

		Random rand = new Random();
		String code = String.valueOf(rand.nextInt(1000000));
		code = "000000" + code;
		code = code.substring(code.length() - 6);
		String content = "您的验证码是：" + code + "。请不要把验证码泄露给其他人。";
		boolean result = smsUtil.sendSms(mobile, content);
		// boolean result = true;
		if (result) {
			inviteCode = new InviteCode();
			inviteCode.setCode(code);
			inviteCode.setPhone(mobile);
			inviteCode.setGmtCreate(new Date());
			inviteCode.setGmtModify(new Date());
			inviteCode.setEffectiveTime(DateUtils.addSeconds(new Date(), 300));
			inviteCode.setStatus(0);
			inviteCode.setType(0);
			this.inviteCodeService.insertInviteCode(inviteCode);
			return code;
		}
		return null;
	}

//	/**
//	 * 团购的验证码
//	 * 
//	 * @param trade
//	 * @param mobile
//	 * @return
//	 */
//	public String sendGroupBuyCode(Trade trade, String mobile) {
//
//		List<MallTuanCode> mallTuanCodeList = this.mallTuanCodeService.getMallTuanCodeListByTradeAndUserId(trade.getBuyUserId(), trade.getId());
//		String content = null;
//		MallTuanCode mallTuanCode = null;
//		for (int i = 0; i < mallTuanCodeList.size(); i++) {
//			mallTuanCode = mallTuanCodeList.get(i);
//			content=mallTuanCode.getCode()+","+content;
//		}
//		if(mallTuanCodeList.size()>0){
//			content = content.substring(0,content.length()-1);
//		}
//		
//		content = "[城市助手]您的团购验证码：" + content + "，请及时使用！";
//		boolean result = smsUtil.sendSms(mobile, content);
//		if (result == false) {
//			return "";
//		}
//		return "succ";
//	}
	/**
	 * 民宿用户下单给卖家提醒
	 * @param mobile
	 * @param content
	 * @return
	 */
	public String sendMessToSeller(String mobile,String content) {

		

		content = "[城市助手]亲爱的店家，" + content + "下单了，请到后台查看！";
		boolean result = smsUtil.sendSms(mobile, content);
		if (result == false) {
			return "";
		}
		return "succ";
	}

	public static void main(String[] args) {
		System.out.println(DateUtils.addMilliseconds(new Date(), 300));
	}

	/**
	 * 校验邀请码
	 * 
	 * @param mobile
	 * @param valid
	 * @return
	 */
	public boolean checkVerifyCode(String mobile, String valid) {
		if (StringUtils.isBlank(mobile) || StringUtils.isBlank(valid)) {
			return false;
		}
		if (WHITENO.equals(valid)) {
			return true;
		}
		InviteCode inviteCode = this.inviteCodeService.getInviteCodeByPhone(mobile);
		if (inviteCode == null) {
			return false;
		}
		if (valid.equals(inviteCode.getCode())) {
			inviteCode.setStatus(-4);
			inviteCode.setGmtModify(new Date());
			inviteCode.setReason("使用");
			inviteCodeService.deleteInviteCodeByPhone(mobile, inviteCode);
			return true;
		}
		return false;
	}

	public boolean checkVerifyYzm(String valid) {
		if (StringUtils.isBlank(valid)) {
			return false;
		}
		InviteCode inviteCode = this.inviteCodeService.getInviteCodeByValid(valid);
		if (inviteCode == null) {
			return false;
		}
		if (valid.equals(inviteCode.getCode())) {
			inviteCode.setStatus(-4);
			inviteCode.setGmtModify(new Date());
			inviteCode.setReason("使用");
			inviteCodeService.updateInviteCodeByInviteCode(inviteCode);
			;
			return true;
		}
		return false;
	}

}
